<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { Lock, User } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
const { push } = useRouter()
import { Login } from '@/api/index'
import { objectToQueryParams } from '@/utils/objectToQuery'
import { useMessage } from '@/hooks/useMessage'

const message = useMessage()

const form = ref({
  UserID: '',
  UserPW: ''
})

const onSubmit = async () => {
  const post = {
    groupName: 'UserMgt',
    jobType: 'Login',
    operType: 'Login',
    userID: form.value.UserID,
    userPW: form.value.UserPW
  }
  const inputStr = objectToQueryParams(post)
  const res = (await Login({ inputStr })) as any
  if (res.code !== 200) {
    message.error(res.msg)
    return
  }
  localStorage.setItem('SCADA_USER', form.value.UserID)
  message.success(res.msg)
  push({ path: '/' })
}

onMounted(() => {
  const user = localStorage.getItem('SCADA_USER')
  form.value.UserID = user || ''
})
</script>

<template>
  <div class="login-bg-box">
    <div class="mask"></div>
    <div class="login-main-box">
      <div class="login-img-box">
        <img src="../../assets/imgs/login_bg.png" />
      </div>
      <div class="login-right-box">
        <h4>SCADA管理系统</h4>
        <el-form :model="form" label-width="auto" style="max-width: 600px; margin: 30px 0">
          <el-form-item label="工号">
            <el-input
              size="large"
              :prefix-icon="User"
              placeholder="请输入工号"
              v-model="form.UserID"
              tabindex="1"
            />
          </el-form-item>
          <el-form-item label="密码">
            <el-input
              size="large"
              type="password"
              :prefix-icon="Lock"
              placeholder="请输入密码"
              v-model="form.UserPW"
              show-password
              tabindex="2"
              @keydown.enter="onSubmit"
            />
          </el-form-item>
        </el-form>
        <el-button
          color="#243ccf"
          size="large"
          style="width: 100%"
          round
          type="primary"
          @click="onSubmit"
          >登录</el-button
        >
      </div>
    </div>
  </div>
</template>

<style scoped>
.login-bg-box {
  position: relative;
  height: 100vh;
  width: 100vw;
  padding-top: 150px;
  /* background: linear-gradient(to right, #243ccf, #1a509a); */
}
.login-bg-box .mask {
  position: absolute;
  top: 0;
  width: 100%;
  height: 100%;
  background: url('../../assets/imgs/login-bg.jpg') no-repeat center center;
  background-size: 100%;
  filter: blur(3px);
  opacity: 0.8;
}
.login-main-box {
  position: absolute;
  display: flex;
  justify-content: space-around;
  align-items: center;
  width: 40%;
  height: 447px;
  padding-right: 20px;
  left: 50%;
  margin-left: -20%;
  top: 50%;
  margin-top: -200px;
  background: rgb(255, 255, 254, 0.9);
  border-radius: 30px;
}
.login-img-box {
  width: 50%;
  margin: 0;
}
.login-right-box h4 {
  font-size: 24px;
  font-weight: bold;
  letter-spacing: 5px;
  color: #4256ce;
  margin: 0 20px;
  text-align: center;
}
</style>
